# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Carlo Landmeter <clandmeter@alpinelinux.org>
pkgname=net-snmp
pkgver=5.9.4
pkgrel=1
pkgdesc="Simple Network Management Protocol"
url="http://www.net-snmp.org/"
arch="all"
license="Net-SNMP"
depends_dev="openssl-dev>3"
# we need perl-net-snmp and perl-tk in make depends for bootstrapping issues
# net-snmp-gui subpackage has perl-net-snmp and perl-tk as depends
makedepends="file perl-dev openssl-dev>3 perl-net-snmp perl-tk linux-headers"
subpackages="$pkgname-dbg $pkgname-doc $pkgname-dev $pkgname-libs $pkgname-agent-libs:alibs
	$pkgname-perl:pl $pkgname-gui::noarch $pkgname-tools $pkgname-openrc"
source="https://downloads.sourceforge.net/net-snmp/net-snmp-$pkgver.tar.gz
	netsnmp-swinst-crash.patch
	fix-includes.patch
	fix-fd_mask.patch
	fix-pkgconf-version.patch

	snmpd.initd
	snmpd.confd
	snmptrapd.confd
	"
options="!check"

# secfixes:
#   5.9.3-r2:
#     - CVE-2015-8100
#     - CVE-2022-44792
#     - CVE-2022-44793
#   5.9.3-r0:
#     - CVE-2022-24805
#     - CVE-2022-24806
#     - CVE-2022-24807
#     - CVE-2022-24808
#     - CVE-2022-24809
#     - CVE-2022-24810

prepare() {
	default_prepare
	# Allow tmpfs volume size monitoring
	# Ref #932
	sed -e 's#"tmpfs",#/*  "tmpfs",  */#g' -i agent/mibgroup/host/hr_filesys.c
}

build() {
	export lt_cv_sys_max_cmd_len=8192

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--with-default-snmp-version="3" \
		--with-sys-contact="root@unknown" \
		--with-sys-location="unknown" \
		--with-logfile="/var/log/net-snmpd.log" \
		--enable-ucd-snmp-compatibility \
		--with-persistent-directory="/var/lib/net-snmp" \
		--with-openssl \
		--enable-pic \
		--enable-ipv6 \
		--enable-shared \
		--enable-as-needed \
		--enable-blumenthal-aes \
		--with-perl-modules="INSTALLDIRS=vendor" \
		--disable-embedded-perl
	# embedded-perl seems to create TEXTREL's

	make
}

package() {
	make -j1 DESTDIR="$pkgdir" install
	# remove things we dont want distribute
	rm "$pkgdir"/usr/lib/libsnmp* \
		"$pkgdir"/usr/bin/snmpcheck \
		"$pkgdir"/usr/bin/fixproc \
		"$pkgdir"/usr/share/man/man1/fixproc* \
		"$pkgdir"/usr/bin/ipf-mod.pl \
		"$pkgdir"/usr/bin/snmpinform
	ln -s snmptrap "$pkgdir"/usr/bin/snmpinform

	install -m755 -D "$srcdir"/snmpd.initd "$pkgdir"/etc/init.d/snmpd
	ln -s snmpd "$pkgdir"/etc/init.d/snmptrapd
	install -m644 -D "$srcdir"/snmpd.confd "$pkgdir"/etc/conf.d/snmpd
	install -m644 -D "$srcdir"/snmptrapd.confd \
		"$pkgdir"/etc/conf.d/snmptrapd
	install -m640 -D EXAMPLE.conf "$pkgdir"/etc/snmp/snmpd.conf
	echo "authCommunity log,execute,net public" > "$pkgdir"/etc/snmp/snmptrapd.conf
	mkdir -p "$pkgdir"/var/lib/net-snmp
	find "$pkgdir" -name perllocal.pod -delete
}

libs() {
	pkgdesc="The NET-SNMP runtime client libraries"
	replaces="net-snmp"
	mkdir -p "$subpkgdir"/usr/lib "$subpkgdir"/usr/share/snmp
	mv "$pkgdir"/usr/lib/libnetsnmp.so.* "$subpkgdir"/usr/lib/
	mv "$pkgdir"/usr/share/snmp/mibs "$subpkgdir"/usr/share/snmp/
}

alibs() {
	pkgdesc="The NET-SNMP runtime agent libraries"
	replaces="net-snmp"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libnetsnmpagent*.so.* \
		"$pkgdir"/usr/lib/libnetsnmphelpers*.so.* \
		"$pkgdir"/usr/lib/libnetsnmpmibs*.so.* \
		"$pkgdir"/usr/lib/libnetsnmptrapd*.so.* \
		"$subpkgdir"/usr/lib/
}

pl() {
	pkgdesc="The perl NET-SNMP module and the mib2c tool"
	replaces="net-snmp"
	mkdir -p "$subpkgdir"/usr/lib \
		"$subpkgdir"/usr/bin \
		"$subpkgdir"/usr/share/snmp

	mv "$pkgdir"/usr/bin/mib2c-update \
		"$pkgdir"/usr/bin/mib2c \
		"$pkgdir"/usr/bin/snmp-bridge-mib \
		"$pkgdir"/usr/bin/net-snmp-cert \
		"$pkgdir"/usr/bin/traptoemail \
		"$subpkgdir"/usr/bin/
	mv "$pkgdir"/usr/lib/perl* "$subpkgdir"/usr/lib/
	mv "$pkgdir"/usr/share/snmp/mib2c* \
		"$pkgdir"/usr/share/snmp/*.pl \
		"$subpkgdir"/usr/share/snmp/
}

gui() {
	pkgdesc="An interactive graphical MIB browser for SNMP"
	depends="net-snmp-perl perl-tk"
	replaces="net-snmp-tools"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/tkmib "$subpkgdir"/usr/bin/
}

tools() {
	pkgdesc="Network management utilities using SNMP"
	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/bin "$subpkgdir"/usr
}

sha512sums="
a510fa91a21e9ddc86a12fd1d0bc6b356e63f3ea53f184d2e31439004d41d902390664134dc40b3b828eabb4282eaf3da628a07c4d480fa00eff7e700950c423  net-snmp-5.9.4.tar.gz
4ad92f50b14d5e27ba86256cc532a2dd055502f4d5fbb1700434f9f01f881fd09bb1eadb94e727554e1470f036707558314c64a66d0376b54e71ab31d5e4baa3  netsnmp-swinst-crash.patch
87a552bd2e41684bba6e87fbcf6454a85ee912d7a339411fda24cebddf7661f0856729e076a917920a542cf84b687ffd90a091daa15f2c48f0ff64f3a53c0ddb  fix-includes.patch
6b70e65c16c8b5dea81b7aa12155a7d99e647e35b9e3c20c546850fe5a745061bf6ea2985e5405aa0a657fb0931e62264f68be2f55def4ecff1ecb83f26731c8  fix-fd_mask.patch
e05c82c9d7a2fe1616996f3641d0ea7907ce17133623e771ec5742a161fd94b43b2e4475ae2d9372c4ec93467b3398865c9b0d0aaa501ff7aa4eebf4c9ef3299  fix-pkgconf-version.patch
1616d471105fc004bd7472d244d4ff9eae936875853bc51381ce2ddbb4bc29825beab6ed01299c76e3230b5725083ae169381f4572d12f015f6c682926b708bc  snmpd.initd
ca4921190e8b81e3244465fa66bf6f0a075add159c8ec13a1198ded155df89eb984a48e34972b20f549fbfa9bbff4a24d66bf7cd0b51f5f86a26ad4c2f5cf5cc  snmpd.confd
073fd2b83eedd6eda1f7345350268ce7946ef6d67a8f26f7c232e46feb75babf68272ae12071a2f9ea76ede71393b3ae4672d3cd47cfd14ab77e3a6482f2e124  snmptrapd.confd
"
